<html>
<head><meta charset="utf-8"><title>how __rust_alloc and friends are defined · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/how.20__rust_alloc.20and.20friends.20are.20defined.html">how __rust_alloc and friends are defined</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="204925165"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/how%20__rust_alloc%20and%20friends%20are%20defined/near/204925165" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jason Hu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/how.20__rust_alloc.20and.20friends.20are.20defined.html#204925165">(Jul 24 2020 at 14:50)</a>:</h4>
<p>Hi all,</p>
<p>I am looking into dynamic memory of rust and I see that they eventually go to <code>__rust_alloc</code> and friends. but the LLVM code simply leaves them declared. how are these function implemented?</p>



<a name="204925629"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/how%20__rust_alloc%20and%20friends%20are%20defined/near/204925629" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lzutao <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/how.20__rust_alloc.20and.20friends.20are.20defined.html#204925629">(Jul 24 2020 at 14:54)</a>:</h4>
<p>They are defined here: <a href="https://github.com/rust-lang/rust/blob/900869371e13cead086f4f9809419daa6a63cfaf/src/libstd/alloc.rs#L326-L379">https://github.com/rust-lang/rust/blob/900869371e13cead086f4f9809419daa6a63cfaf/src/libstd/alloc.rs#L326-L379</a></p>



<a name="204926200"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/how%20__rust_alloc%20and%20friends%20are%20defined/near/204926200" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/how.20__rust_alloc.20and.20friends.20are.20defined.html#204926200">(Jul 24 2020 at 14:58)</a>:</h4>
<p>The <code>__rust_*</code> functions are defined in <a href="https://github.com/rust-lang/rust/blob/master/src/librustc_codegen_llvm/allocator.rs">https://github.com/rust-lang/rust/blob/master/src/librustc_codegen_llvm/allocator.rs</a>. They dispatch to either <code>__rdl_alloc</code> or <code>__rg_alloc</code> depending on if <code>#[global_allocator]</code> is used or the default libstd implementation.</p>



<a name="204926252"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/how%20__rust_alloc%20and%20friends%20are%20defined/near/204926252" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/how.20__rust_alloc.20and.20friends.20are.20defined.html#204926252">(Jul 24 2020 at 14:58)</a>:</h4>
<p>The <code>__rg_alloc</code> methods are expanded from <code>#[global_allocator]</code> by <a href="https://github.com/rust-lang/rust/blob/8534be72fc3b9c5f2f2dc4e4ee7b651a008e9a3e/src/librustc_builtin_macros/global_allocator.rs">https://github.com/rust-lang/rust/blob/8534be72fc3b9c5f2f2dc4e4ee7b651a008e9a3e/src/librustc_builtin_macros/global_allocator.rs</a></p>



<a name="204926661"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/how%20__rust_alloc%20and%20friends%20are%20defined/near/204926661" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jason Hu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/how.20__rust_alloc.20and.20friends.20are.20defined.html#204926661">(Jul 24 2020 at 15:01)</a>:</h4>
<p>thx for the pointer. then it seems to go to <code>GlobalAlloc::alloc</code> but it's just a trait. how this function gets injected then?</p>



<a name="204926952"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/how%20__rust_alloc%20and%20friends%20are%20defined/near/204926952" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/how.20__rust_alloc.20and.20friends.20are.20defined.html#204926952">(Jul 24 2020 at 15:03)</a>:</h4>
<p>In the <code>__rdl_alloc</code> case, <code>System.alloc()</code> is called. In the <code>__rg_alloc</code> case <code>.alloc()</code> is called on the static that has the <code>#[global_allocator]</code> attribute.</p>



<a name="204927138"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/how%20__rust_alloc%20and%20friends%20are%20defined/near/204927138" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lzutao <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/how.20__rust_alloc.20and.20friends.20are.20defined.html#204927138">(Jul 24 2020 at 15:04)</a>:</h4>
<p>For system allocators, on Unix, it is listed here: <a href="https://github.com/rust-lang/rust/blob/900869371e13cead086f4f9809419daa6a63cfaf/src/libstd/sys/unix/alloc.rs#L6-L24">https://github.com/rust-lang/rust/blob/900869371e13cead086f4f9809419daa6a63cfaf/src/libstd/sys/unix/alloc.rs#L6-L24</a></p>



<a name="204927481"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/how%20__rust_alloc%20and%20friends%20are%20defined/near/204927481" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jason Hu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/how.20__rust_alloc.20and.20friends.20are.20defined.html#204927481">(Jul 24 2020 at 15:07)</a>:</h4>
<p>thx, that's really cool</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>